/******************************************************************************
 * FileName:    gwin_remote_pingtouxiong.h
 * Desc:        遥控
 *                  平头熊达协议
 *
 *
 * Author:      Lin
 * Notes:
 *
 ******************************************************************************/

/*-------------------------- Includes ---------------------------------------*/
#include "gwin_board.h"
#include "gwin_remote.h"
#include "gwin_main_task.h"
#include "gwin_protocol_task.h"

/*-------------------------- Macro Definitions ------------------------------*/
#define LOCAL_DEBUG_EN                      ( 0 )
#if LOCAL_DEBUG_EN
#define GWIN_DEBUG(...)                     USER_DEBUG(__VA_ARGS__)
#else
#define GWIN_DEBUG(...)
#endif


#define GWIN_COMPANY_ID_1                   0x48
#define GWIN_COMPANY_ID_2                   0x53
#define GWIN_COMPANY_ID_3                   0x43
#define GWIN_COMPANY_ID_4                   0x58


#define GWIN_RB_CMD_BRIGHTNESS_MIN          0x80
#define GWIN_RB_CMD_BRIGHTNESS_MAX          0xE4
#define GWIN_RB_CMD_COLOUR_TEMP_MIN         0x1B
#define GWIN_RB_CMD_COLOUR_TEMP_MAX         0x41
#define GWIN_RB_CMD_COLOUR_TEMP_UNIT        100


/*-------------------------- Type Declarations ------------------------------*/
/**
 * @brief 遥控协议 偏移
 *
 *      遙控协议  Adv Data单元格式: Len + Type + Data
 *          Adv Data : 0x11 0xFF 数据
 *                                  厂商ID（4Byte） + 遥控id（6Byte） + 分组id（1Byte） + 按键id（1Byte） + 命令参数（3Byte） + 命令累加（1Byte）
 */
typedef enum
{
    GWIN_REMOTE_PROT_INDEX_LEN,
    GWIN_REMOTE_PROT_INDEX_TYPE,
    GWIN_REMOTE_PROT_INDEX_COMPANY_1,
    GWIN_REMOTE_PROT_INDEX_COMPANY_2,
    GWIN_REMOTE_PROT_INDEX_COMPANY_3,
    GWIN_REMOTE_PROT_INDEX_COMPANY_4,
    GWIN_REMOTE_PROT_INDEX_REMOTE_1,
    GWIN_REMOTE_PROT_INDEX_REMOTE_2,
    GWIN_REMOTE_PROT_INDEX_REMOTE_3,
    GWIN_REMOTE_PROT_INDEX_REMOTE_4,
    GWIN_REMOTE_PROT_INDEX_REMOTE_5,
    GWIN_REMOTE_PROT_INDEX_REMOTE_6,
    GWIN_REMOTE_PROT_INDEX_GROUP,
    GWIN_REMOTE_PROT_INDEX_BTN,
    GWIN_REMOTE_PROT_INDEX_DATA_1,
    GWIN_REMOTE_PROT_INDEX_DATA_2,
    GWIN_REMOTE_PROT_INDEX_DATA_3,
    GWIN_REMOTE_PROT_INDEX_CMD,
    GWIN_REMOTE_PROT_INDEX_MAX,
}gwin_remote_prot_index_t;
#define GWIN_REMOTE_PROT_ID_1                               0x01

/**
 * @brief 遥控命令2
 */
typedef enum
{
    GWIN_RB_CMD2_SCENE_3                        = 0x00,     // 情景模式3
    GWIN_RB_CMD2_SLEEP                          = 0x9F,     // 助眠
    GWIN_RB_CMD2_WAKEUP                         = 0xA7,     // 唤醒
}gwin_remote_btn_cmd2_t;

/*-------------------------- Global Variables -------------------------------*/

/*-------------------------- Local Variables --------------------------------*/
/**
 * @brief 遙控协议头
 *          Adv Data单元格式: Len + Type + Data
 *          Adv Data1 + Adv Data2
 */
static const uint8_t remote_prot_head[] = { 0x11, 0xFF, GWIN_COMPANY_ID_1, GWIN_COMPANY_ID_2, GWIN_COMPANY_ID_3, GWIN_COMPANY_ID_4, };
static const uint8_t remote_prot_head_cnt = sizeof(remote_prot_head);


/*-------------------------- Functions Declarations -------------------------*/

/*-------------------------- Functions Definitions --------------------------*/
/**
 * @brief 遥控 判断协议
 */
int8_t gwin_remote_date_check_pingtouxiong(uint8_t *data, uint16_t len, remote_pair_info_t *remote_pair_info, uint8_t *adv_payload_pos, uint8_t *cmd_index)
{
    uint8_t find_head = 0;
    uint8_t _adv_payload_pos = 0;
    adv_payload_unit_t *adv_payload_unit = NULL;
    while (_adv_payload_pos < len)
    {
        adv_payload_unit = (adv_payload_unit_t *)&data[_adv_payload_pos];
        if (adv_payload_unit->len < ADV_PAYLOAD_UNIT_LEN_MIN            // 防止不按照蓝牙格式
                || adv_payload_unit->len > ADV_PAYLOAD_UNIT_LEN_MAX)    // 防止不按照蓝牙格式
        {
            break;
        }
        if (memcmp(remote_prot_head, adv_payload_unit, remote_prot_head_cnt) == 0)
        {
            find_head = 1;
            break;
        }
        _adv_payload_pos += (adv_payload_unit->len + 1);
    }
    if (!find_head)
    {
        return -1;
    }
    data = (uint8_t *)adv_payload_unit;

    *adv_payload_pos = _adv_payload_pos;
    remote_pair_info->group_id = 0x0000;
    remote_pair_info->no_match_group = 1;
    *cmd_index = data[GWIN_REMOTE_PROT_INDEX_CMD];

    return 0;
}


/**
 * @brief 遥控接收数据
 */
void gwin_remote_date_proc_pingtouxiong(uint8_t *data, uint16_t len, remote_pair_info_t *remote_pair_info, uint8_t *_action, uint32_t *_param)
{
//    uint8_t cmd_id = 0;
 //   uint8_t btn_id = 0;
    uint8_t action = 0;
    uint32_t param = 0;

    // 数据解析
//    cmd_id = data[GWIN_REMOTE_PROT_INDEX_CMD];
//    btn_id = data[GWIN_REMOTE_PROT_INDEX_BTN];

    uint8_t data_remote_to_uart[31] = { 0x00 };
    memcpy(data_remote_to_uart, data, len < sizeof(data_remote_to_uart) ? len : sizeof(data_remote_to_uart));
    gwin_protocol_data_recv(PROTOCOL_TASK_DATA_WAY_PINGTUOXIONG_REMOTE, data_remote_to_uart, len);

    GWIN_DEBUG("%02x%02x%02x%02x%02x%02x : cmd_id = 0x%02x, btn_id = 0x%02x",
                                    remote_pair_info->remote_id[0], remote_pair_info->remote_id[1], remote_pair_info->remote_id[2], remote_pair_info->remote_id[3], remote_pair_info->remote_id[4], remote_pair_info->remote_id[5], cmd_id, btn_id);
    GWIN_DEBUG("data : %02x %02x %02x", data[GWIN_REMOTE_PROT_INDEX_DATA_1], data[GWIN_REMOTE_PROT_INDEX_DATA_2], data[GWIN_REMOTE_PROT_INDEX_DATA_3]);

    if (action > 0)
    {
        *_action = action;
        *_param = param;
    }
}


/*-------------------------- END OF FILE ------------------------------------*/

